package com.lgt_enterprise_android.view.pages.me;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.adapter.PoiSearchAdapter;
import com.lgt_enterprise_android.base.BaseActivity;

import java.util.ArrayList;

public class SearchShopActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener {

    private ListView mListView;
    private PoiSearchAdapter adapter;

    private PoiSearch poiSearch;
    PoiSearch.Query query;

    private ArrayList<PoiItem> listPoiItem=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_shop);

        mListView=(ListView)findViewById(R.id.listview);
        adapter=new PoiSearchAdapter(mContext,new ArrayList<PoiItem>());
        mListView.setAdapter(adapter);

        poiSearch=new PoiSearch(mContext,query);
        poiSearch.setOnPoiSearchListener(this);

        ((TextView)findViewById(R.id.ed_search)).addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
//                query = new PoiSearch.Query(editable.toString(), "", "");
//                poiSearch.setQuery(query);
//                poiSearch.searchPOIAsyn();

                PoiSearch.Query query = new PoiSearch.Query(editable.toString(),"","");
                query.setPageSize(50);// 设置每页最多返回多少条poiitem
                query.setPageNum(1);// 设置查第一页
                query.setCityLimit(true);
                poiSearch = new PoiSearch(mContext,query);
                poiSearch.setOnPoiSearchListener(SearchShopActivity.this);
                poiSearch.searchPOIAsyn();

            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent();
                ShopAddressActivity.currentPoiItme=listPoiItem.get(i);
                intent.putExtra("name",listPoiItem.get(i).toString());
                intent.putExtra("location",listPoiItem.get(i).getLatLonPoint().toString());
                setResult(Activity.RESULT_OK,intent);
                finish();
            }
        });
    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int rCode) {
        if (rCode== AMapException.CODE_AMAP_SUCCESS){
            listPoiItem=poiResult.getPois();
            adapter.setData(listPoiItem);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
